‘МІНІСТЕРСТВО ОСВІТИ І НАУКИ УКРАЇНИ
НАЦІОНАЛЬНИЙ УНІВЕРСИТЕТ „ЛЬВІВСЬКА ПОЛІТЕХНІКА”
Лабораторна робота №2
з дисципліни " Теорія інтелектуальних систем"
Львів – 2014
Назва: Моделювання простих форм цілеспрямованої поведінки. Дослідження роботи цілеспрямованих автоматів (Learning Automata) у стаціонарному випадковому середовищі.
МЕТА: Змоделювати та дослідити роботу цілеспрямованого автомату у стаціонарному випадковому середовищі.
N
Модель оптимальної поведінки
Конструкція ЦА
Кількість доступних агенту дій
8
W1
AL
3
AL -> Автомат з лінійною тактикою (автомат М.Л.Цетліна)
// environment
int environment (int _en)
{
int _r = 0;
switch (_en)
{
case 0: _r = seResponse(); break;
case 1: _r = ceResponse(); break;
default: printf("lab2 error: wrong env code specified\n");
}
return _r;
}
// ----------------------------------------------------------------------------
// save results of random agent
void saveResultsRA (void)
{
int i;
if ((RA_res_file = fopen(RA_res_file_name,"w")) == NULL)
fprintf(stderr, "Cannot open file <%s> for experimental results.\n", RA_res_file_name);
for (i=0; i < T; i++)
fprintf(RA_res_file,"%f,%f,%f,%f\n", sumRm[i], sumRv[i], avrRm[i], avrRv[i]);
fclose(RA_res_file);
}
// ----------------------------------------------------------------------------
// save results of perfect agent
void saveResultsPA (void)
{
int i;
if ((PA_res_file = fopen(PA_res_file_name,"w")) == NULL)
fprintf(stderr, "Cannot open file <%s> for experimental results.\n", PA_res_file_name);
for (i=0; i < T; i++)
fprintf(PA_res_file,"%f,%f,%f,%f\n", sumRm[i], sumRv[i], avrRm[i], avrRv[i]);
fclose(PA_res_file);
}
// ----------------------------------------------------------------------------
// save results of learning automaton
void saveResultsLA (void)
{
int i;
if ((LA_res_file = fopen(LA_res_file_name,"w")) == NULL)
fprintf(stderr, "Cannot open file <%s> for experimental results.\n", LA_res_file_name);
for (i=0; i < T; i++)
fprintf(LA_res_file,"%f,%f,%f,%f\n", sumRm[i], sumRv[i], avrRm[i], avrRv[i]);
fclose(LA_res_file);
}
// ----------------------------------------------------------------------------
// return index of maximal value in <_array>
int argmax(float* _array, int size)
{
int _arg = uRand(size);
float _max = _array[_arg];
for (int i=0; i < size; i++)
if (_array[i] > _max) {_max = _array[i]; _arg = i;}
return _arg;
}
// ----------------------------------------------------------------------------
// init agent
void initAgent (int _ag)
{
switch (_ag)
{
case 0: break;
case 1: break;
case 2: state = 1; action = uRand(nA); break;
case 3: state = 1; action = uRand(nA); break;
case 4: state = 1; action = uRand(nA); break;
default: printf("lab2 error: wrong agent code specified\n");
}
}
// ----------------------------------------------------------------------------
// random agent
int randomAgent (void)
{
return uRand(nA);
}
// ----------------------------------------------------------------------------
// perfect agent
int perfectAgent (void)
{
if (env) paction = argmax(cePa[ceState],nA);
else paction = argmax(sePa,nA);
return paction;
}
// ----------------------------------------------------------------------------
// learning automaton with linear tactics (Tsetlin's automaton)
int LLA (void)
{
int _action = action;
if (response > 0) // 1 -> reward
{
if (state < memSize) state++; // step up in current branch
}
else // 0 -> penalty
{
if (state == 1)
{
// change action (change branch of automaton)
if (action == (nA-1)) _action = 0;
else _action = action + 1;
}
else state--; // step down in current branch
}
/*
// compact version
(response>0)?
((state<memSize)?state++:state):
((state==1)?
((action==(nA-1))?
(_action=0):
(_action++)):
(state--));
// one line version
int r=response, s=state, m=memSize, a=action;
(r>0)?((s<m)?s++:s):((s==1)?((a==...